﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Datos
{
    [MetadataType(typeof(ProveedorMetaData))]
    public partial class Proveedor
    {
        Repository<ArqParcial1Entities> repositorio = new Repository<ArqParcial1Entities>();
        ArqParcial1Entities context = new ArqParcial1Entities();

        public List<Proveedor> Listar()
        {
            var result = from proveedor in context.Proveedors
                         select proveedor;
            return result.ToList();
        }

        public Proveedor Buscar(int iCodigo)
        {
            var result = from proveedor in context.Proveedors
                         where proveedor.provCodigo == iCodigo
                         select proveedor;
            return result.Count() > 0 ? result.First() : new Proveedor();
        }

        public List<Proveedor> Consultar(int? iCodigo, string sNombre, string sDireccion)
        {
            var query = from proveedor in context.Proveedors
                        select proveedor;
            if (iCodigo == null && string.IsNullOrWhiteSpace(sNombre) && string.IsNullOrWhiteSpace(sDireccion))
            {
                return new List<Proveedor>();
            }
            else
            {
                if (iCodigo != null)
                {
                    query = query.Where(w => w.provCodigo == iCodigo);
                }
                if (!string.IsNullOrWhiteSpace(sNombre))
                {
                    query = query.Where(w => w.provNombre.Contains(sNombre));
                }
                if (!string.IsNullOrWhiteSpace(sDireccion))
                {
                    query = query.Where(w => w.provDireccion.Contains(sDireccion));
                }

                return query.ToList();
            }
        }

        public bool Guardar(Proveedor proveedor)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Insert<Proveedor>(proveedor);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Actualizar(Proveedor proveedor)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Update<Proveedor>(proveedor);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Eliminar(Proveedor proveedor)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Delete<Proveedor>(proveedor);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
    }
    class ProveedorMetaData
    {
    }
}
